package com.lxu.report.service;

import com.lxu.report.entity.YwReportData;
import com.lxu.report.entity.enums.ReportEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TemplateNameAssembleService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public String assembleTemplateName(YwReportData ywReportData){
        String templateTypeCode = ywReportData.getTemplateTypeCode();
        String id = ywReportData.getId();
        if(StringUtils.isBlank(templateTypeCode)){
            throw new RuntimeException("模板类型编号(templateTypeCode)不能为空");
        }
        if(StringUtils.isBlank(id)){
            throw new RuntimeException("编号(id)不能为空");
        }
        //id在过程中变化了，通过ExportRecordId进行存储
        if(StringUtils.isNotBlank(ywReportData.getExportRecordId())){
            id = ywReportData.getExportRecordId();
        }
        String reportCode ="";
        switch (ReportEnum.TEMPLATE_TYPE.valueOf(templateTypeCode)){
            case LXBG:
                reportCode = getNameById(id);
                break;
        }
        //String time = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());
        return reportCode+"_"+ywReportData.getProjectId();
    }

    private String getNameById(String id) {
        String strSql = "select project_name from yw_proj_detail where id='"+id+"'";
        return jdbcTemplate.queryForObject(strSql,String.class);
    }
}
